const express = require('express')
const router = express.Router()
const db = require('./db')
const api = require('./api')
const check = require('./check')
const func = require('./func')
const checkLogin = check.checkLogin
const checkNotLogin = check.checkNotLogin

// 注册接口
router.post(api.signup, function(req, res) {
    let name = req.body.name;
    let password = req.body.password;
    let query1 = `select name from user where name = '${name}'`;
    let query2 = `insert into user (name, password) VALUES('${name}', '${password}')`;
    let _res = res;
    func.connPool(query1).then((res) => {
        if (res.length != 0) {
            _res.json({ code: '002', msg: 'user is exist' })
            return
        }
        func.connPool(query2).then((res) => {
            _res.json({ code: '000', msg: 'success' })
        }).catch((err) => {
            console.error(err)
            _res.json({ code: '002', msg: 'sql fail' })
        })
    }).catch((err) => {
        console.error(err)
        _res.json({ code: '002', msg: 'sql fail' })
    })
})

//登录接口
router.post(api.signin, function(req, res) {
    let name = req.body.name;
    let password = req.body.password;
    let query1 = `select name from user where name = '${name}'`;
    let query2 = `select * from user where name = '${name}' and password = '${password}'`;

    let _res = res;
    func.connPool(query1).then((res) => {
        if (res.length != 0) {

            func.connPool(query2).then((res) => {
                if (res.length != 0) {

                    _res.json({ code: '000', msg: '登录成功。' })
                } else {

                    _res.json({ code: '001', msg: '密码错误。' })
                }
            })
        } else {

            _res.json({ code: '001', msg: '账号不存在。' })
        }
    }).catch((err) => {
        console.error(err)
        _res.json({ code: '002', msg: 'sql fail' })
    })
})

// 保存文章
router.post(api.saveArticle, function(req, res) {
    let title = req.body.title;
    let date = req.body.date;
    let content = req.body.content;
    let gist = req.body.gist;
    let labels = req.body.labels;
    let _res = res;
    let query = `insert into article (title, date, content, gist, labels) values('${title}','${date}','${content}','${gist}','${labels}')`;

    func.connPool(query).then((res) => {

        _res.json({ code: '000', msg: '文章保存成功。' })
    }).catch((err) => {
        console.error(err)
        _res.json({ code: '002', msg: 'sql fail' })
    })
})

//获取所有文章
router.post(api.getArticles, function(req, res) {
    let query = `select id,title,date,gist,labels from article`;
    let _res = res;
    func.connPool(query).then((res) => {

        _res.json(res);
    }).catch((err) => {
        console.error(err)
        _res.json({ code: '002', msg: 'sql fail' })
    })
})

// 文章详情页
router.post(api.getArticleDetail, function(req, res) {
    let id = req.body.id;
    let query = `select * from article where id = ${id}`;

    let _res = res;
    func.connPool(query).then((res) => {

        _res.json(res);
    }).catch((err) => {

        console.error(err)
        _res.json({ code: '002', msg: 'sql fail' })
    })
})

router.post(api.deleteArticle, function(req, res) {
    let id = req.body.id;
    let query = `delete from article where id = ${id}`;
    let _res = res;

    func.connPool(query).then((res) => {

        _res.json({ code: '000', msg: 'success' })
    }).catch((err) => {

        console.error(err)
        _res.json({ code: '002', msg: 'sql fail' })
    })
})


// // 文章更新
// router.post('/api/admin/updateArticle', function (req, res) {
//   let info = req.body.articleInformation
//   db.Article.find({_id: info._id}, function (err, docs) {
//     if (err) {
//       return
//     }
//     docs[0].title = info.title
//     docs[0].date = info.date
//     docs[0].content = info.content
//     docs[0].gist = info.gist
//     docs[0].labels = info.labels
//     db.Article(docs[0]).save(function (err) {
//       if (err) {
//         res.status(500).send()
//         return
//       }
//       res.send()
//     })
//   })
// })



module.exports = router